package com.xfcy.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xfcy.content.mapper.CourseTeacherMapper;
import com.xfcy.content.model.dto.AddCourseTeacherDto;
import com.xfcy.content.model.dto.EditCourseTeacherDto;
import com.xfcy.content.model.po.CourseTeacher;
import com.xfcy.content.service.CourseTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 晓风残月Lx
 * @date 2023/6/16 19:10
 */
@Service
@Slf4j
public class CourseTeacherServiceImpl implements CourseTeacherService {

    @Resource
    private CourseTeacherMapper courseTeacherMapper;

    /**
     * 查询老师
     *
     * @param courseId
     * @return
     */
    @Override
    public CourseTeacher getCourseTeacher(Long courseId) {
        return courseTeacherMapper.selectOne(new LambdaQueryWrapper<CourseTeacher>().eq(CourseTeacher::getCourseId, courseId));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public CourseTeacher addCourseTeacher(AddCourseTeacherDto addCourseTeacherDto) {
        CourseTeacher courseTeacher = new CourseTeacher();
        BeanUtils.copyProperties(addCourseTeacherDto, courseTeacher);
        int insert = courseTeacherMapper.insert(courseTeacher);
        return courseTeacher;
    }

    /**
     * 修改老师信息
     * @param editCourseTeacherDto
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public CourseTeacher editCourseTeacher(EditCourseTeacherDto editCourseTeacherDto) {
        CourseTeacher courseTeacher = courseTeacherMapper.selectById(editCourseTeacherDto.getId());
        BeanUtils.copyProperties(editCourseTeacherDto, courseTeacher);
        courseTeacherMapper.updateById(courseTeacher);
        return courseTeacher;
    }

    @Override
    public void deleteTeacher(Long courseId, Long teacherId) {
        courseTeacherMapper.deleteById(teacherId);
    }


}
